package com.apes.fn.ssm.repository;

import com.apes.fn.ssm.model.BrandPriority;
import com.apes.fn.ssm.model.FittingMessage;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.stock.model.Location;
import org.elasticsearch.action.ValidateActions;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface BrandPriorityRepository extends CustomRepository<BrandPriority, String> {
    @Query(value = "SELECT A.ID FROM MAS_BRAND_PRIORITY A", nativeQuery = true)
    List findAllId();

    @Query(value = "" +
            "SELECT DISTINCT LOCATION_ID " +
            "  FROM MAS_CALCULATE_PRODUCT A " +
            " WHERE NOT EXISTS (SELECT 1 " +
            "          FROM MAS_CALCULATED_STATE J " +
            "         WHERE J.LOCATION_ID = A.LOCATION_ID " +
            "           AND DATA_CENTER_MODEL_MARK2 = 2 " +
            "           AND DATA_CENTER_MODEL_MARK = 2) ", nativeQuery = true)
    List<Map> distinctLocation();

    @Query(value = "SELECT A.BRAND_ID FROM MAS_BRAND_PRIORITY A WHERE A.FITTING_NAME_ID = :fittingId AND A.LOCATION_ID = :locationId AND A.VALID = '1'", nativeQuery = true)
    List<String> findAllByBrand(@Param("fittingId") String fittingId,@Param("locationId") String locationId);

    @Query(value = "SELECT A.FITTING_NAME_ID FROM MAS_BRAND_PRIORITY A WHERE A.LOCATION_ID = :locationId AND VALID = 1 ", nativeQuery = true)
    List<String> findAllFittingMessage(@Param("locationId") String locationId);

    @Query(value = "SELECT A.ID, A.VALID, A.VERSION, (SELECT B.ID FROM mas_location B WHERE B.ID = A.location_id) LOCATION_ID,\n" +
            "(SELECT C.ID FROM mas_enumeration C WHERE C.ID = A.priority) PRIORITY,\n" +
            "(SELECT D.ID FROM mas_fitting_message D WHERE D.ID = A.fitting_Name_id) FITTING_NAME FROM MAS_BRAND_PRIORITY A " +
            "WHERE A.LOCATION_ID = :locationId AND A.FITTING_NAME_ID = :fittingNameId " +
            "AND A.BRAND_ID = :brandId", nativeQuery = true)
    Map<String, String> queryByCondition(@Param("locationId") String locationId,
                                         @Param("fittingNameId")String fittingNameId,
                                         @Param("brandId")String brandId);
}
